<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="goodsQueryParams.query"
            clearable
            @clear="getGoodsList"
          >
            <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="6">
          <el-button type="primary" @click="goAddPage">添加商品</el-button>
        </el-col>
      </el-row>

      <!-- 商品列表区域 -->
      <el-table border stripe :data="goodsList">
        <el-table-column type="index"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name" width="700px"></el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" width="140px">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="delGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 数据列表分页器 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        background
        :current-page="goodsQueryParams.pagenum"
        :page-sizes="[2, 5, 8, 10]"
        :page-size="goodsQueryParams.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 商品列表数组
      goodsList: [],
      // 商品列表查询参数对象
      goodsQueryParams: {
        query: '',
        pagenum: 1,
        pagesize: 8
      },
      //   数据总页数
      total: null
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    // 获取商品列表
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', {
        params: this.goodsQueryParams
      })
      if (res.meta.status !== 200) {
        return this.$message.error('商品列表获取失败')
      }
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 点击分页器改变页面最大显示数据条数时触发的函数
    handleSizeChange(pagesize) {
      this.goodsQueryParams.pagesize = pagesize
      this.getGoodsList()
    },
    // 点击分页器改变当前页时触发的函数
    handleCurrentChange(pagenum) {
      this.goodsQueryParams.pagenum = pagenum
      this.getGoodsList()
    },
    // 点击删除按钮删除该商品
    async delGoods(id) {
      // 弹出询问框
      this.$confirm('您确定要永久删除该商品吗,删除后数据不可恢复', '提示', {
        type: 'warning',
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${id}`)
          if ((res.meta.status !== 200)) {
            return this.$message.error(res.meta.msg)
          }
          this.getGoodsList()
          return this.$message.success('商品删除成功')
        })
        .catch(() => {
          return this.$message.info('操作已取消')
        })
    },
    // 点击添加商品按钮跳转至添加商品页面
    goAddPage() {
        this.$router.push('/goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
